package com.tg.app.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

public class ForeSightView extends View {
    PointF mPointF;
    Paint mPaint;
    Paint rPaint;
    Paint bgPaint;
    Rect rect;
    float mRadius = 60;
    float bulge = 15;
    GestureDetector gestureDetector;
    OnForeSightViewListener listener;
    boolean isChanged ;
    public void setListener(OnForeSightViewListener listener) {
        this.listener = listener;
    }

    public ForeSightView(Context context) {
        super(context);
        setupView(context);
    }

    public ForeSightView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setupView(context);
    }

    public ForeSightView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setupView(context);
    }

    private  void setupView(Context context){
        rPaint = new Paint();
        rPaint.setStrokeWidth(1.5f);
        rPaint.setColor(Color.WHITE);
        rPaint.setStyle(Paint.Style.STROKE);

        mPaint = new Paint();
        mPaint.setStrokeWidth(2f);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(Color.parseColor("#66ffffff"));


        bgPaint = new Paint();
        bgPaint.setStyle(Paint.Style.FILL);
        bgPaint.setColor(Color.parseColor("#33ffffff"));
        gestureDetector = new GestureDetector(context,new GestureDetector.SimpleOnGestureListener(){


            @Override
            public boolean onDown(MotionEvent e) {
                if (Math.abs(mPointF.x-e.getX())>mRadius)return false;
                if (Math.abs(mPointF.y-e.getY())>mRadius)return false;
                return true;
            }
            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                Log.i("GestureDetector","onScroll");

                if (mPointF==null)return false;
                if (e2.getPointerCount() == 1) {
                    float dx = mPointF.x-distanceX;
                    float dy = mPointF.y-distanceY;
                    float left = dx-mRadius;
                    float top = dy-mRadius;
                    float right = dx+mRadius;
                    float bottom = dy+mRadius;
                    if (left<0||top<0||right>getWidth()||bottom>getHeight())return false;
                    mPointF.set(dx,dy);
                    invalidate();
                    isChanged = true;
                    return true;
                }
                return false;
            }

        });
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        boolean handled = gestureDetector.onTouchEvent(event);
        if (event.getAction()==MotionEvent.ACTION_UP&&isChanged){
            if (listener!=null){
                float x = mPointF.x/getWidth();
                float y = mPointF.y/getHeight();
                isChanged = false;
                listener.onChanged(x,y);
            }
        }
       return handled;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mPointF==null){
            mPointF = new PointF(getWidth()/2,getHeight()/2);
        }

        Log.d("onDraw","mPointF.x "+mPointF.x+" mPointF.y: "+mPointF.y);
        rect = new Rect();
        int left = (int) (mPointF.x-mRadius);
        int top = (int) (mPointF.y-mRadius);
        int right = (int) (mPointF.x+mRadius);
        int bottom = (int) (mPointF.y+mRadius);
        rect.set(left,top,right,bottom);
        canvas.drawLine(0,mPointF.y,left+bulge,mPointF.y,mPaint);
        canvas.drawLine(mPointF.x,0,mPointF.x,top+bulge,mPaint);
        canvas.drawLine(right-bulge,mPointF.y,getWidth(),mPointF.y,mPaint);
        canvas.drawLine(mPointF.x,bottom-bulge,mPointF.x,getHeight(),mPaint);
        canvas.drawRect(rect,bgPaint);
        canvas.drawRect(rect,rPaint);
    }
    public interface  OnForeSightViewListener{
        void  onChanged(float x ,float y);
    }
}
